package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.ShiroPermission;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:17
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class ShiroPermissionDao extends BaseDao<ShiroPermission> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<ShiroPermission> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<ShiroPermission> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<ShiroPermission> list = run.query(sql, new ResultSetHandler<List<ShiroPermission>>() {
            @Override
            public List<ShiroPermission> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    ShiroPermission shiroPermission = new ShiroPermission();

                    shiroPermission.setId(rs.getInt("id"));
                    shiroPermission.setDescription(rs.getString("description"));
                    shiroPermission.setName(rs.getString("name"));
                    shiroPermission.setParentId(rs.getInt("parent_id"));
                    shiroPermission.setVersion(rs.getInt("version"));
                    shiroPermission.setWeight(rs.getInt("weight"));

                    list.add(shiroPermission);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  ShiroPermission 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public ShiroPermission selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, description, name, parent_id, version, weight from shiro_permission where id = ?";
        ShiroPermission shiroPermission = run.query(sql,new ResultSetHandler<ShiroPermission>() {
            @Override
            public ShiroPermission handle(ResultSet rs) throws SQLException {
                ShiroPermission shiroPermission = null;
                if(rs.next()) {
                    shiroPermission = new ShiroPermission();

                        shiroPermission.setId(rs.getInt("id"));
                        shiroPermission.setDescription(rs.getString("description"));
                        shiroPermission.setName(rs.getString("name"));
                        shiroPermission.setParentId(rs.getInt("parent_id"));
                        shiroPermission.setVersion(rs.getInt("version"));
                        shiroPermission.setWeight(rs.getInt("weight"));
                }
                return  shiroPermission;
            }
        },params);
        return shiroPermission;
    }

    /**
    *
    * @param shiroPermission 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(ShiroPermission shiroPermission) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO shiro_permission (  ");
        sql.append("  description, name, parent_id, version, weight ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            shiroPermission.getDescription(),  
            shiroPermission.getName(),  
            shiroPermission.getParentId(),  
            shiroPermission.getVersion(),  
            shiroPermission.getWeight()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  shiroPermission
    * @return
    * @throws SQLException
    */
    @Override
    public int update(ShiroPermission shiroPermission) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   shiro_permission           ");
        sql.append(" SET                    ");
            
            sql.append("   description  = ?,  ");
            sql.append("   name  = ?,  ");
            sql.append("   parent_id  = ?,  ");
            sql.append("   version  = ?,  ");
            sql.append("   weight  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                shiroPermission.getDescription(), 
                shiroPermission.getName(), 
                shiroPermission.getParentId(), 
                shiroPermission.getVersion(), 
                shiroPermission.getWeight(), 
            shiroPermission.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from shiro_permission where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
